/* icintra.p - COMMON PROGRAM FOR MISC INVTY TRANSACTIONS CONTINUED         */
/* Copyright 1986 QAD Inc. All rights reserved.                               */
/* $Id:: icintra.p 85749 2019-04-25 02:34:43Z cfs                          $: */
/* REVISION: 7.0      LAST MODIFIED: 09/18/91   BY: pma *F003*                */
/* REVISION: 6.0      LAST MODIFIED: 11/11/91   BY: WUG *D887*                */
/* REVISION: 7.3      LAST MODIFIED: 09/27/92   By: jcd *G247*                */
/* REVISION: 7.3      LAST MODIFIED: 11/30/92   BY: pma *G366*                */
/* REVISION: 7.3      LAST MODIFIED: 03/20/93   BY: pma *G852*                */
/* REVISION: 7.4      LAST MODIFIED: 09/01/93   BY: dpm *H075*                */
/* REVISION: 7.4      LAST MODIFIED: 09/11/94   BY: rmh *GM10*                */
/* REVISION: 7.3      LAST MODIFIED: 10/05/94   BY: pxd *FR90*                */
/* REVISION: 8.5      LAST MODIFIED: 11/02/94   BY: ktn *J038*                */
/* REVISION: 8.5      LAST MODIFIED: 03/29/95   BY: dpm *J044*                */
/* REVISION: 8.5      LAST MODIFIED: 03/29/95   BY: dzn *F0PN*                */
/* REVISION: 8.5      LAST MODIFIED: 04/26/95   BY: sxb *J04D*                */
/* REVISION: 8.5      LAST MODIFIED: 07/31/95   BY: taf *J053*                */
/* REVISION: 8.6      LAST MODIFIED: 10/19/96   BY: *K003* Vinay Nayak-Sujir  */
/* REVISION: 8.6      LAST MODIFIED: 03/15/97   BY: *K04X* Steve Goeke        */
/* REVISION: 8.6      LAST MODIFIED: 04/15/97   BY: *K08N* Vinay Nayak-Sujir  */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* Annasaheb Rahane   */
/* REVISION: 8.6E     LAST MODIFIED: 05/09/98   BY: *L00Y* Jeff Wootton       */
/* REVISION: 9.1      LAST MODIFIED: 10/01/99   BY: *N014* Patti Gaultney     */
/* REVISION: 9.1      LAST MODIFIED: 08/13/00   BY: *N0KS* Mark Brown         */
/* REVISION: 9.1      LAST MODIFIED: 09/27/00   BY: *N0W6* Mudit Mehta        */
/* Revision: 1.13     BY: Ellen Borden            DATE: 10/24/01  ECO: *P00G* */
/* Revision: 1.15     BY: Patrick Rowan           DATE: 05/24/02  ECO: *P018* */
/* Revision: 1.18     BY: Steve Nugent            DATE: 06/06/02  ECO: *P07Y* */
/* Revision: 1.19     BY: Patrick Rowan           DATE: 06/21/02  ECO: *P08K* */
/* Revision: 1.20     BY: Dorota Hohol            DATE: 02/25/03  ECO: *P0N6* */
/* Revision: 1.22     BY: Paul Donnelly (SB)      DATE: 06/26/03  ECO: *Q00G* */
/* Revision: 1.23     BY: Robin McCarthy          DATE: 04/19/04  ECO: *P15V* */
/* Revision: 1.24     BY: Russ Witt               DATE: 06/21/04  ECO: *P1CZ* */
/* Revision: 1.25     BY: Ed van de Gevel         DATE: 18/03/05  ECO: *R00F* */
/* Revision: 1.26     BY: Shivaraman V.           DATE: 11/24/05  ECO: *P48W* */
/* Revision: 1.27     BY: Tejasvi Kulkarni        DATE: 01/17/06  ECO: *Q0PP* */
/* Revision: 1.28     BY: Masroor Alam            DATE: 08/18/06  ECO: *P52L* */
/* Revision: 1.31     BY: Katie Hilbert           DATE: 03/01/07  ECO: *R0C6* */
/* Revision: 1.32     BY: Neil Curzon             DATE: 03/10/08  ECO: *R0P6* */
/* Revision: 1.34     BY: Nan Zhang               DATE: 02/02/08  ECO: *R0JS* */
/* Revision: 1.37     BY: Nan Zhang               DATE: 08/04/08  ECO: *R0ZR* */
/* Revision: 1.38     BY: Jiang Wan               DATE: 12/20/08  ECO: *R1BY* */
/* Revision: 1.39     BY: Zhijun Guan             DATE: 09/08/09  ECO: *R1V9* */
/* Revision: 1.40     BY: Xiaolei Meng            DATE: 05/21/10  ECO: *R22Q* */
/* $Revision: 1.41 $ BY: Huaping Luo         DATE: 03/10/10 ECO: *R21W* */

/*-Revision end---------------------------------------------------------------*/

/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

{us/bbi/mfdeclre.i}
{us/gp/gpuid.i}
{us/bbi/gplabel.i} /* EXTERNAL LABEL INCLUDE */
{us/px/pxmaint.i}
{us/px/pxphdef.i ictrxr}
{us/px/pxphdef.i caisxr}
{us/px/pxphdef.i caatdxr}
{us/px/pxphdef.i cahistxr}
{us/px/pxphdef.i captdxr2}
{us/px/pxphdef.i gputlxr}
{us/px/pxphdef.i icilxr}

{us/ic/icshtxut.i} /* GTM tax util */

{us/ca/caattr.i}
{us/ca/cattdef.i}
{us/ca/cattdef2.i}

define input parameter shipnbr    like tr_ship_id      no-undo.
define input parameter ship_date  like tr_ship_date    no-undo.
define input parameter inv_mov    like tr_ship_inv_mov no-undo.
define input parameter kbtransnbr as   integer         no-undo.
define input parameter i_addship  as   logical         no-undo.
define input-output parameter table for tt_attr_pfvalue.

define new shared variable  ec_ok as logical.

define shared variable transtype as character format "x(7)".
define shared variable ref like glt_ref.
define shared variable issrct as character format "x(3)".
define shared variable um like pt_um no-undo.
define shared variable conv like um_conv label "Conversion" no-undo.
define shared variable eff_date like glt_effdate label "Effective".
define shared variable trlot like tr_lot.
define shared variable unit_cost like tr_price label "Unit Cost".
define shared variable ordernbr like tr_nbr.
define shared variable orderline like tr_line.
define shared variable so_job like tr_so_job.
define shared variable traddr like tr_addr.
define shared variable rmks like tr_rmks.
define shared variable dr_acct like trgl_dr_acct.
define shared variable dr_sub like trgl_dr_sub.
define shared variable dr_cc like trgl_dr_cc.
define shared variable cr_acct like trgl_cr_acct.
define shared variable cr_sub like trgl_cr_sub.
define shared variable cr_cc like trgl_cr_cc.
define shared variable project like wo_proj no-undo.
define shared variable lotserial_qty like sr_qty no-undo.
define shared variable site like sr_site no-undo.
define shared variable part like tr_part.
define shared variable tr_recno as recid.

define variable trans-ok like mfc_logical.
define variable wonbr like lot_nbr.
define variable woline like lot_line.
define variable gl_tmp_amt as decimal no-undo.
define variable v_abs_recid as recid no-undo.
define variable v_trflow_flg       as   logical    no-undo.
/* CONSIGNMENT VARIABLES. */
{us/so/socnvars.i}

/* trgl_det account variables */
{us/ic/icglacct.i}

define variable consign_inv_acct as character no-undo.
define variable consign_inv_sub as character no-undo.
define variable consign_inv_cc as character no-undo.
define variable consign_intrans_acct as character no-undo.
define variable consign_intrans_sub as character no-undo.
define variable consign_intrans_cc as character no-undo.
define variable consign_offset_acct as character no-undo.
define variable consign_offset_sub as character no-undo.
define variable consign_offset_cc as character no-undo.
define variable glcost as decimal             no-undo.
define variable assay  as character           no-undo.
define variable grade  as character           no-undo.
define variable expire as date                no-undo.
define variable io_first_time as logical initial yes no-undo.
define variable hold_trnbr like tr_trnbr      no-undo.
define variable l_rmks as character no-undo.
define variable io_batch like cnsu_batch no-undo.
define variable using_supplier_consignment as logical no-undo.
define variable temp_loc         as character no-undo.
define variable v_unconfirm_flg  as logical   no-undo.
define variable oidLdgMstr as decimal no-undo.
define variable LdgNbr as character no-undo.
define variable l-messageArgs  as   character  no-undo.
define variable l-context      as   character  no-undo.
define variable l-retry        as   logical    no-undo.

/* SHARED TEMP-TABLES */
{us/ic/icshmtdf.i "new"}

l_rmks = getTermLabel("CONSIGNED",12).

find first gl_ctrl where gl_domain = global_domain no-lock.

find first clc_ctrl where clc_domain = global_domain no-lock no-error.
if not available clc_ctrl then do:
   {us/bbi/gprun.i ""gpclccrt.p""}
   find first clc_ctrl where clc_domain = global_domain no-lock.
end.

/* CHECK TO SEE IF CUSTOMER CONSIGNMENT IS ACTIVE */
{us/bbi/gprun.i ""gpmfc01.p""
         "(input ENABLE_CUSTOMER_CONSIGNMENT,
           input 10,
           input ADG,
           input CUST_CONSIGN_CTRL_TABLE,
           output using_cust_consignment)"}

/* DETERMINE IF SUPPLIER CONSIGNMENT IS ACTIVE */
{us/bbi/gprun.i ""gpmfc01.p""
         "(input 'enable_supplier_consignment',
           input 11,
           input ADG,
           input 'cns_ctrl',
           output using_supplier_consignment)"}

/* Read item record */
find pt_mstr
   where pt_domain = global_domain
   and   pt_part = part
no-lock no-error.

/* Clear shipper line item temp table */
if i_addship then do:
   {us/bbi/gprun.i  ""icshmt1c.p""}
end.  /* if i_addship */

for each sr_wkfl
   where sr_domain = global_domain
   and   sr_userid = SessionUniqueID
exclusive-lock:
   assign
      lotserial_qty = sr_qty * conv
      wonbr  = ""
      woline = "".

   if issrct = "ISS" then lotserial_qty = - lotserial_qty.

   if (transtype = "RCT-PO" or transtype = "RCT-WO") then
      assign
         wonbr  = ordernbr
         woline = sr_lineid.

   /* ADD RECORD TO LOT MASTER */
   if (clc_lotlevel <> 0) and (sr_lotser <> "") then do:
      {us/bbi/gprun.i ""gpiclt.p""
               "(input part,
                 input sr_lotser,
                 input wonbr,
                 input woline,
                 output trans-ok)"}

      if not trans-ok then do:
         /* CURRENT TRANSACTION REJECTED - */
         {us/bbi/pxmsg.i &MSGNUM=2740 &ERRORLEVEL=4}
         delete sr_wkfl.
         next.               /* CONTINUE WITH NEXT TRANSACTION */
      end.
   end. /* IF (CLC_LOTLEV <> 0) */

   /* Add to shipper line item temp table */
   if i_addship then do:

      temp_loc  = sr_loc.

      {us/bbi/gprun.i ""icshmt1a.p""
               "(input ordernbr,
                 input orderline,
                 input part,
                 input sr_lotser,
                 input sr_ref,
                 input sr_site,
                 input sr_loc,
                 input sr_qty,
                 input um,
                 input conv,
                 input if available pt_mstr
                       then
                          pt_net_wt * sr_qty * conv
                       else
                          0,
                 input if available pt_mstr
                       then
                          pt_net_wt_um
                       else
                          """",
                 input if available pt_mstr
                       then
                          pt_size * sr_qty * conv
                       else
                          0,
                 input if available pt_mstr
                       then
                          pt_size_um
                       else
                          """" )"}

   end.  /* if i_addship */

end.  /* for each sr_wkfl */

/* Global Shipping */
/* Create or add to shipper */
if i_addship and lotserial_qty <> 0 then do:

   if transtype matches "ISS*" then do:
      /* Record the account data user input */
      run RecordAccount(input dr_acct,
                        input dr_sub,
                        input dr_cc,
                        input project).

      /* Get unconfirmShipperFlg from Shipper Control, and */
      /* display a message for user confirmation */
      {us/gp/gprunp.i "soldxr" "p" "getUnconfirmShipperFlg"
         "(input site,
      input traddr)"}

      {us/bbi/gprun.i ""icshmt.p""
            "(input site,
              input traddr,
              input transtype,
              input eff_date,
              ""yes"",
              output v_abs_recid,
              output v_trflow_flg)" }
             
      /* DELETE qad_wkfl RECORDS CREATED */
      {us/px/pxrun.i &PROC='p-del-qadwkfl' &PROGRAM='ictrxr.p'
                     &HANDLE=ph_ictrxr
                     &PARAM="(input '"icshmt.p"',
                              input SessionUniqueID)"} 

      find first abs_mstr where recid(abs_mstr) = v_abs_recid no-lock no-error.
      /* COLLECT INFO FOR THE LEGAL DOCUMENT */
      if available abs_mstr then do:
         {us/gp/gprunp.i "soldxr" "p" "saveLegalSiteLoc"
            "(input abs_id,
              input site,
              input temp_loc,
              input traddr,
              input """")"}
      end.
   end.
   else if transtype matches "RCT*" then do:
      /* Record the account data user input */
      run RecordAccount(input cr_acct,
                        input cr_sub,
                        input cr_cc,
                        input project).

      /* Get unconfirmShipperFlg from Shipper Control, and */
      /* display a message for user confirmation */
      {us/gp/gprunp.i "soldxr" "p" "getUnconfirmShipperFlg"
         "(input traddr,
           input site)"}

      {us/bbi/gprun.i ""icshmt.p""
            "(input traddr,
              input site,
              input transtype,
              input eff_date,
              input ""yes"",
              output v_abs_recid,
              output v_trflow_flg)" }

      /* DELETE qad_wkfl RECORDS CREATED */
      {us/px/pxrun.i &PROC='p-del-qadwkfl' &PROGRAM='ictrxr.p'
                     &HANDLE=ph_ictrxr
                     &PARAM="(input '"icshmt.p"',
                              input SessionUniqueID)"} 

      find first abs_mstr where recid(abs_mstr) = v_abs_recid no-lock no-error.
      /* COLLECT INFO FOR THE LEGAL DOCUMENT */
      if available abs_mstr then do:
         {us/gp/gprunp.i "soldxr" "p" "saveLegalSiteLoc"
            "(input abs_id,
              input traddr,
              input """",
              input site,
              input temp_loc)"}
      end.
   end.

   /* Get associated shipper */
   find abs_mstr where recid(abs_mstr) = v_abs_recid
   no-lock no-error.

   if available abs_mstr then do:
      assign
         shipnbr   = substring(abs_id,2)
         ship_date = abs_shp_date
         inv_mov   = abs_inv_mov.
   end.

   /* Delete the account data in work table */
   run DeleteAccount.
end.  /* if i_addship */

assign
   oidLdgMstr = 0
   LdgNbr = "".
for first qad_wkfl no-lock where
     qad_wkfl.qad_domain = SessionUniqueID and
     qad_wkfl.qad_key1 = "UpdateHistTransactions" and
     qad_wkfl.qad_key2 = "LegalDocumentIDs":
     assign
        oidLdgMstr = qad_wkfl.qad_decfld[1]
        LdgNbr = qad_wkfl.qad_charfld[1].
end.

   /* processTran-loop will first make sure it can get exclusive lock      */
   /* on all the inventory records it needs to process. If it can't get    */
   /* exclusive lock on all records it will release the locks and retry    */
   /* until it has all of them locked. The processing of the business      */
   /* logic will not start until it has all the records locked.            */

processTran-loop:
do transaction on error undo, retry:
/*This line of code in order is required to invoke expected retry behaviour */
   if retry then .
/* For these transactions, we must first lock the required inventory data   */
/* to ensure the entire process is completed before releasing the data      */
/* we need to lock both in_mstr and ld_det data                             */

   if retry then l-retry = true.
   else l-retry = false.

   for each sr_wkfl
      where sr_domain = global_domain
      and   sr_userid = SessionUniqueID
   no-lock:

      l-context = sr_site + "," + part + "," + sr_loc + "," + sr_lotser +
                  "," + sr_ref.

      /* hasMaxNumOfLockRetryAttemptsBeenExceeded is a general purpose */
      /* procedure to keep track of the number of tries that have been made*/
      /* to lock the entire set of in_mstr & ld_det records. Once the */
      /* maximum number of attempts to lock the set of records has been */
      /* reached, this procedure will return {&APP-ERROR-RESULT} */
      {us/px/pxrun.i &PROC = 'hasMaxNumOfLockRetryAttemptsBeenExceeded'
         &PROGRAM = 'gputlxr.p'     /* General Utility RESPONSIBILITY-OWNING PROGRAM */
         &HANDLE=ph_gputlxr
         &PARAM = "(input l-retry,
                    input l-messageArgs,
                    input l-context)"
         &NOAPPERROR = true
         &CATCHERROR = true}
      if return-value <> {&SUCCESS-RESULT} then
          undo processTran-loop, leave processTran-loop.

      /* Lock the in_mstr and ld_det data to be processed */
      {us/px/pxrun.i &PROC = 'lockLocationDetailInventoryData'
         &PROGRAM = 'icilxr.p'       /* INVENTORY LOCATION RESPONSIBILITY-OWNING PROGRAM*/
         &HANDLE=ph_icilxr
         &PARAM = "( input part,
         input sr_site,
         input sr_loc,
         input sr_lotser,
         input sr_ref,
         output l-messageArgs)"
         &NOAPPERROR = true
         &CATCHERROR = true}
      if return-value = {&APP-ERROR-RESULT} then
         undo processTran-loop, retry processTran-loop.

   end. /* for each sr_wkfl, set to lock inv data before processing */

   for each sr_wkfl
      where sr_domain = global_domain
      and   sr_userid = SessionUniqueID
   no-lock:
      lotserial_qty = sr_qty * conv.
      if issrct = "ISS" then lotserial_qty = - lotserial_qty.

      if issrct = "RCT" then do:
         if lotserial_qty > 0 then do:
            /* Check Receipt Result against attribute specification */
            {us/px/pxrun.i &PROC      ='validateAttrSpec'
                           &PROGRAM   ='caatdxr.p'
                           &HANDLE    = ph_caatdxr
                           &PARAM     ="(input sr_wkfl.oid_sr_wkfl,
                                       input-output table tt_attr_pfvalue by-reference)"
                           &NOAPPERROR=true
                           &CATCHERROR=true
            }

            /* Update the attributes are saved into inventory during receipt. */
            {us/px/pxrun.i &PROC       = 'updateAttrPfValueRCTSkipSave'
                           &PROGRAM    = 'caatdxr.p'
                           &HANDLE     = ph_caatdxr
                           &PARAM      = "(input sr_wkfl.oid_sr_wkfl,
                                           input-output table tt_attr_pfvalue by-reference)"
                           &NOAPPERROR = true
                           &CATCHERROR = true
            }
         end.
         else do:
            /* Check negative receipt or return Result */
            {us/px/pxrun.i &PROC      ='validateAttrSpec'
                           &PROGRAM   = 'caisxr.p'
                           &HANDLE    = ph_caisxr
                           &PARAM     ="(input sr_wkfl.oid_sr_wkfl,
                                       input-output table tt_attr_pfvalue by-reference)"
                           &NOAPPERROR=true
                           &CATCHERROR=true
            }
            /* Update the attributes are saved into inventory during negative receipt
             * or return. */
            {us/px/pxrun.i &PROC       = 'updateAttrPfValueISSSkipSave'
                           &PROGRAM    = 'caisxr.p'
                           &HANDLE     = ph_caisxr
                           &PARAM     ="(input sr_wkfl.oid_sr_wkfl,
                                           input-output table tt_attr_pfvalue by-reference)"
                           &NOAPPERROR = true
                           &CATCHERROR = true
            }
         end.

      end.
      /* Save the attribute value to temp-table tt_attrdh_hist */
      {us/px/pxrun.i &PROC='recordAttrHistInitial'
                     &PROGRAM='cahistxr.p'
                     &HANDLE=ph_cahistxr
                     &PARAM="(input sr_wkfl.oid_sr_wkfl,
                              input no,
                              input table tt_attr_pfvalue by-reference,
                              input-output table tt_attrdh_hist by-reference)"
                     &NOAPPERROR=true
                     &CATCHERROR=true
      }

      {us/ic/ictrans.i
         &addrid=traddr
         &bdnstd=0
         &cracct="
           if issrct = ""RCT"" then cr_acct
           else
              if available pld_det then pld_inv_acct
              else pl_inv_acct"
         &crsub="if issrct = ""RCT"" then cr_sub
                 else
                    if available pld_det then pld_inv_sub
                    else pl_inv_sub"
         &crcc="if issrct = ""RCT"" then cr_cc
                else
                   if available pld_det then pld_inv_cc
                   else pl_inv_cc"
         &crproj=project
         &curr=""""
         &dracct="if issrct = ""ISS""  then dr_acct
                  else
                     if available pld_det then pld_inv_acct
                     else pl_inv_acct"
         &drsub="if issrct = ""ISS""  then dr_sub
                 else
                    if available pld_det then pld_inv_sub
                    else pl_inv_sub"
         &drcc="if issrct = ""ISS""  then dr_cc
                else
                   if available pld_det then pld_inv_cc
                   else pl_inv_cc"
         &drproj=project
         &effdate=eff_date
         &exrate=0
         &exrate2=0
         &exratetype=""""
         &exruseq=0
         &glamt="if available sct_det then sct_cst_tot * sr_qty * conv
                 else 0"
         &kbtrans=kbtransnbr
         &lbrstd=0
         &line=orderline
         &location=sr_loc
         &lotnumber=trlot
         &lotref=sr_ref
         &lotserial=sr_lotser
         &mtlstd=0
         &ordernbr=ordernbr
         &ovhstd=0
         &part=part
         &perfdate=?
         &price="if available sct_det then sct_cst_tot
                 else 0"
         &quantityreq=0
         &quantityshort=0
         &quantity=lotserial_qty
         &revision=""""
         &rmks=rmks
         &shiptype=""""
         &shipnbr=shipnbr
         &shipdate=ship_date
         &invmov=inv_mov
         &site=sr_site
         &slspsn1=""""
         &slspsn2=""""
         &sojob=so_job
         &substd=0
         &transtype=transtype
         &msg=0
         &ref_site=tr_site
         &mgmt_curr=""""
         &mgmt_curr_amt=0
         &mgmt_curr_ex_rate=0
         &mgmt_curr_ex_rate2=0
         &mgmt_curr_ex_ratetype=""""}



      /* Save the attribute value and attribute History. */
      {us/px/pxrun.i &PROC       = 'saveInvAttrValue'
                     &PROGRAM    = 'caatdxr.p'
                     &HANDLE     = ph_caatdxr
                     &PARAM      = "(input sr_wkfl.oid_sr_wkfl,
                                     input 0,       /* oid trans parent */
                                     input table tt_attr_pfvalue by-reference)"
                     &NOAPPERROR = true
                     &CATCHERROR = true
      }

      /* Save the new attribute value to DB as history. */
      {us/px/pxrun.i &PROC='recordAttrHistChanged'
                     &PROGRAM='cahistxr.p'
                     &HANDLE=ph_cahistxr
                     &PARAM="(input sr_wkfl.oid_sr_wkfl,
                              input sr_wkfl.oid_sr_wkfl,
                              input tr_hist.tr_trnbr,
                              input yes,
                              input table tt_attr_pfvalue by-reference,
                              input-output table tt_attrdh_hist by-reference)"
                     &NOAPPERROR=true
                     &CATCHERROR=true
      }



      assign
         tr_hist.oid_lgd_mstr = oidLdgMstr
         tr_hist.tr_doc_id = LdgNbr
         tr_recno = recid(tr_hist).

      if using_cust_consignment
         and
         ((transtype = "RCT-UNP" and lotserial_qty > 0)
            or
         (transtype = "ISS-UNP" and lotserial_qty < 0))
      then do:
         consign_flag = no.
         {us/gp/gprunmo.i &program = "socnsod1.p" &module = "ACN"
                    &param = """(input ordernbr,
                                 input orderline,
                                 output consign_flag,
                                 output consign_loc,
                                 output intrans_loc,
                                 output max_aging_days,
                                 output auto_replenish)"""}

         if consign_flag then do:

            /* DETERMINE CONSIGNMENT ACCOUNTS */
            if consign_inv_acct = "" then do:
               {us/gp/gprunmo.i &program = "socnacct.p" &module = "ACN"
                          &param = """(input part,
                                       input site,
                                       input sr_loc,
                                       output consign_inv_acct,
                                       output consign_inv_sub,
                                       output consign_inv_cc,
                                       output consign_intrans_acct,
                                       output consign_intrans_sub,
                                       output consign_intrans_cc,
                                       output consign_offset_acct,
                                       output consign_offset_sub,
                                       output consign_offset_cc)"""}
            end.

            /*UPDATE THE RCT-UNP/ISS-UNP REMARK WITH "CONSIGNED"*/
            hold_trnbr = tr_hist.tr_trnbr.
            if rmks = "" then
               for first tr_hist
                   where tr_hist.tr_domain = global_domain
                   and   tr_hist.tr_trnbr = hold_trnbr
               exclusive-lock:
                  tr_rmks = l_rmks.
               end.

            /* CREATE CN-SHIP TRANSACTION */
            /* PASS IN THE RCT_TRNBR TO ICXFER3, TO BE USED FOR REMARKS*/
            {us/gp/gprunmo.i &program = "icxfer3.p" &module = "ACN"
                       &param = """(input trlot,
                                    input sr_lotser,
                                    input sr_ref,
                                    input sr_ref,
                                    input -1 * lotserial_qty,
                                    input ordernbr,
                                    input ordernbr,
                                    input orderline,
                                    input "''",
                                    input project,
                                    input eff_date,
                                    input sr_site,
                                    input sr_loc,
                                    input sr_site,
                                    input sr_loc,
                                    input no,
                                    input "''",
                                    input eff_date,
                                    input inv_mov,
                                    input (if sr_loc = intrans_loc then
                                              consign_intrans_acct
                                           else
                                              consign_inv_acct),
                                    input (if sr_loc = intrans_loc then
                                              consign_intrans_sub
                                           else
                                              consign_inv_sub),
                                    input (if sr_loc = intrans_loc then
                                              consign_intrans_cc
                                           else
                                              consign_inv_cc),
                                    input consign_offset_acct,
                                    input consign_offset_sub,
                                    input consign_offset_cc,
                                    input base_curr,
                                    input (if available sct_det then sct_cst_tot
                                           else 0),
                                    input 0,
                                    output glcost,
                                    input-output hold_trnbr,
                                    input-output assay,
                                    input-output grade,
                                    input-output expire)"""}

                /* CREATE CONSIGNMENT SHIPMENT-INVENTORY X-REF */

                /* PARAMETER io_first_time  gets passed  in as YES initially    */
                /* to & socncix.p. When io_first_time is YES, then socncix.p    */
                /* will create a new cross ref rec.  Once io_first_time    is   */
                /* NO, socncix.p will try to reuse the same cross refernce rec  */
                /* if all other key information is the same.                    */

            /* ADDED 20TH PARAMETER TO INDICATE THE REVERSAL OF THE SHIPMENT */
            {us/gp/gprunmo.i &program = "socncix.p" &module = "ACN"
                       &param = """(input ordernbr,
                                    input orderline,
                                    input sr_site,
                                    input eff_date,
                                    input lotserial_qty,
                                    input um,
                                    input "''",
                                    input hold_trnbr,
                                    input sr_loc,
                                    input sr_lotser,
                                    input sr_ref,
                                    input "''",
                                    input so_job,
                                    input "''",
                                    input "''",
                                    input "''",
                                    input "''",
                                    input "''",
                                    input true,
                                    input no,
                                    input no,
                                    input-output io_first_time)"""}

         end. /* if consign_flag*/
      end. /*if using_cust_consignment*/

      if     using_supplier_consignment
         and transtype = "ISS-UNP"
      then do:

         /* STD TRANSACTION QTY IS PROCESSED AS -lotserial_qty SO IT IS TREATED */
         /* AS A POSITIVE QTY IN ictrancn.p, IMPLYING lotserial_qty IS NEGATIVE */
         /* TO START WITH.  THEREFORE, IF lotserialqty > 0, IT IS A REVERSAL.   */
         {us/gp/gprunmo.i &program = ""pocnsix4.p"" &module = "ACN"
                    &param   =  """(input part,
                                    input sr_site,
                                    input sr_loc,
                                    input right-trim(substring(sr_lotser,1,18)),
                                    input sr_ref,
                                    input (if lotserial_qty > 0 then true
                                           else false),
                                    output consign_flag)"""}
         if consign_flag then do:
            /*UPDATE THE RCT-UNP/ISS-UNP REMARK WITH "CONSIGNED"*/
            hold_trnbr = tr_hist.tr_trnbr.
            if rmks = "" then
               for first tr_hist
                  where tr_hist.tr_domain = global_domain
                  and   tr_hist.tr_trnbr = hold_trnbr
               exclusive-lock:
                  tr_rmks = l_rmks.
               end.

            {us/gp/gprunmo.i &program = ""ictrancn.p"" &module  = "ACN"
                       &param   = """(input ordernbr,
                                      input '',
                                      input 0,
                                      input tr_so_job,
                                      input - lotserial_qty,
                                      input sr_lotser,
                                      input part,
                                      input sr_site,
                                      input sr_loc,
                                      input sr_ref,
                                      input eff_date,
                                      input tr_trnbr,
                                      input false,
                                      input-output io_batch)"""}

         end. /* IF consign_flag */
      end. /*if using_supplier_consignment*/

   end. /* for each sr_wkfl */

   /* Get associated shipper */
   find abs_mstr where recid(abs_mstr) = v_abs_recid
   no-lock no-error.

   if available abs_mstr then do:

      /* The issuing transaction need deal with the Legal Doc */
      if (transtype matches "ISS*" and abs_qty > 0) or
         (transtype matches "RCT*" and abs_qty < 0) then
      do:
         /* If the transaction flow is not internal,
            then print legal document*/
         if v_trflow_flg = yes and not v_unconfirm_flg then do:
            /* Global Shipping: assign Legal Doc nbr */
            {us/bbi/gprun.i ""gpldnbr.p"" "(input v_abs_recid)"}

            /* Print Legal Document */
            {us/bbi/gprun.i ""icldprt.p"" "(recid(abs_mstr))"}
         end.
      end.
      else do:
         /* If the transaction flow is not internal,
            then print legal document*/
         if v_trflow_flg = yes and not v_unconfirm_flg then do:
            /* Complement Salejob and remarks for legal document */
            {us/bbi/gprun.i ""icshldm1.p""
               "(input v_abs_recid,
                 input so_job,
                 input rmks)"}

            /* Print Legal Document */
            {us/bbi/gprun.i ""icldprt.p"" "(input v_abs_recid)"}
         end.
      end.
      run updateTRHistWithLD (input tr_recno).
   end. /* if available abs_mstr */

   {us/gp/gprunp.i "soldxr" "p" "check_unconfirmship"
             "(output v_unconfirm_flg)"}

   if not v_unconfirm_flg and inv_mov <> "" and shipnbr <> "" then do:
      if can-find (first lgd_mstr
                   where lgd_domain = global_domain
                   and lgd_shipper_id = "s" + shipnbr) then do:
         /* Legal Document Tax posting */
         {us/bbi/gprun.i ""icglldtx.p"" "(input v_abs_recid)"}         
      end.
      else do:
         for each tr_hist where tr_domain = global_domain
            and tr_ship_inv_mov = inv_mov
            and tr_ship_id      = shipnbr no-lock
         break by tr_type by tr_nbr by tr_line by tr_part by tr_serial:
            if last-of(tr_serial) then do:
               /* Tax posting */
               {us/bbi/gprun.i ""icgltax.p""
                               "(input tr_trnbr,
                                 input v_abs_recid)"}
            end.
         end.
      end.
   end. /* if not v_unconfirm_flg */

end. /* processTran-loop */

do transaction:
   for each sr_wkfl
      where sr_domain = global_domain
      and   sr_userid = SessionUniqueID
   exclusive-lock:
      delete sr_wkfl.
   end.
end.

PROCEDURE updateTRHistWithLD:
    define input parameter TrHistRecid as recid no-undo.
     
    define buffer tr_hist for tr_hist.
    define buffer lgd_mstr for lgd_mstr.
    
    for first tr_hist exclusive-lock 
       where recid(tr_hist) = TrHistRecid:
           
       if tr_hist.oid_lgd_mstr > 0 then
          for first lgd_mstr no-lock where
             lgd_mstr.oid_lgd_mstr = tr_hist.oid_lgd_mstr:
             if tr_hist.tr_doc_id = "" then
                tr_hist.tr_doc_id = lgd_mstr.lgd_nbr.    
          end.  
       else 
          for first lgd_mstr no-lock where
             lgd_mstr.lgd_domain = global_domain and
             lgd_mstr.lgd_shipper_id = "s" + tr_hist.tr_ship_id:
             if tr_hist.tr_doc_id = "" then
               tr_hist.tr_doc_id = lgd_mstr.lgd_nbr.
             if tr_hist.oid_lgd_mstr = 0 then
                tr_hist.oid_lgd_mstr = lgd_mstr.oid_lgd_mstr.   
          end.
    end.
   
    release tr_hist no-error.
    
END PROCEDURE.  /* updateTRHistWithLD */  
